<?php
namespace Common\Api;
class CommonApi{

	public static function wechatAuthInfo(){
		$access_token = F('access_token');
		if($access_token['expire_in'] < time()){
			$wechatAuth = new WechatAuthApi(C('APPID'), C('APPSECRET'));
			$access_token = $wechatAuth->getAccessToken();
			F('access_token', array('access_token' => $access_token, 'expire_in' => time() + 7000));
			return $wechatAuth;
		}else{
			return new WechatAuthApi(C('APPID'), C('APPSECRET'), $access_token['access_token']);
		}
	}

	static function exportExcel($data, $filename, $download = true){
		$filename = '/stock/Application/Runtime/Temp/' . $filename . '.csv';
		$str = "";
		foreach($data as $key => $value){
			$str .= implode(',', str_replace(array(",", "\n", "\r", "\r\n"), '，', $value)) . "\n";
		}
		file_put_contents($_SERVER['DOCUMENT_ROOT'] . $filename, $str);
		if($download){
			header('Location:' . $filename);
		}else{
			return $filename;
		}
	}
}